{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "30b379af",
   "metadata": {},
   "source": [
    "## 4.线性代数运算（矩阵）\n",
    "\n",
    "这部分常用的库是numpy.linalg()\n",
    "\n",
    "在这一节里面对于matrix和ndarray的区别较为明显，matrix有很多简单的运算符，而array通常需要很套路地一层层调用numpy里面的函数\n",
    "\n",
    "### 1.对整个数组的一般运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "55c5759b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4] [11 12 13 14 15]\n",
      "[0 2 4 6 8]\n",
      "[11 11 11 11 11]\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "v1=np.arange(0,5)\n",
    "v2=np.arange(11,16)\n",
    "print(v1,v2)\n",
    "\n",
    "print(2*v1)\n",
    "print(v2-v1)\n",
    "print(v1+2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "626748a1",
   "metadata": {},
   "source": [
    "### 2. 对数组间元素进行操作\n",
    "\n",
    "如果矩阵的形状并不兼容，那么就会有报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "42ffc968",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.49461706, 0.12745339, 0.02230027, 0.3929219 ],\n",
       "       [0.18881388, 0.00303504, 0.5310738 , 0.92735453],\n",
       "       [0.95464381, 0.00432626, 0.34993478, 0.45872214]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A=np.random.rand(3,4)\n",
    "A*A#element-wise multiplication对应元素逐个相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b62e657",
   "metadata": {},
   "source": [
    "### 3.矩阵乘法\n",
    "\n",
    "**利用dot函数我们可以实现矩阵内元素的乘法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "08a06718",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 5 8]\n",
      " [3 4 8 9]\n",
      " [7 1 8 3]] <class 'numpy.matrix'>\n",
      "[[1 2 5 8 2]\n",
      " [3 4 8 9 4]\n",
      " [7 1 8 3 6]\n",
      " [7 1 8 3 8]]\n",
      "[[ 98  23 125  65 104]\n",
      " [134  39 183 111 142]\n",
      " [ 87  29 131  98  90]]\n"
     ]
    }
   ],
   "source": [
    "#我们定义两个可以相乘的矩阵\n",
    "A=np.array([[1,2,5,8],\n",
    "            [3,4,8,9],\n",
    "            [7,1,8,3]])\n",
    "B=np.array([[1,2,5,8,2],\n",
    "            [3,4,8,9,4],\n",
    "            [7,1,8,3,6],\n",
    "            [7,1,8,3,8]])\n",
    "A=np.matrix(A)\n",
    "print(A,type(A))\n",
    "print(B)\n",
    "print(np.dot(A,B))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaaba26a",
   "metadata": {},
   "source": [
    "**在这里有一个矩阵和数组的区别，数组必须使用dot函数，而矩阵可以直接用星号，因为matrix实际上是array的分支，他的运算符号相对简单一些，但是大部分时候array的方法也是适用matrix的**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4b7912e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 245  395  545]\n",
      " [ 620 1020 1420]\n",
      " [ 995 1645 2295]\n",
      " [1370 2270 3170]]\n",
      "[[ 405  435  465]\n",
      " [ 980 1060 1140]\n",
      " [1555 1685 1815]\n",
      " [2130 2310 2490]]\n"
     ]
    }
   ],
   "source": [
    "A=np.arange(1,21)\n",
    "A=np.matrix(A.reshape(4,5))#这里用了常见的另一种矩阵生成法，数组基础上reshape\n",
    "B=np.arange(11,41,2)\n",
    "B1=np.matrix(B.reshape(3,5))\n",
    "B2=np.matrix(B.reshape(5,3))\n",
    "\n",
    "print(A*B1.T)#3行5列的转置\n",
    "print(A*B2)#5行3列，对应元素不同，结果不同"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcc184cd",
   "metadata": {},
   "source": [
    "### 4.矩阵元素的转换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f83c182",
   "metadata": {},
   "source": [
    "每个元素的共轭（conjugate）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a35b500b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.-2.j, 3.-5.j],\n",
       "        [2.-3.j, 4.-6.j]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A=np.matrix([[1+2j,3+5j],\n",
    "           [2+3j,4+6j]])\n",
    "np.conjugate(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69018a68",
   "metadata": {},
   "source": [
    "厄米共轭：转置+共轭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "88ed081c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.-2.j, 2.-3.j],\n",
       "        [3.-5.j, 4.-6.j]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.H"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6ddf06c",
   "metadata": {},
   "source": [
    "提取实部、复部、模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b44c1a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[2.23606798, 5.83095189],\n",
       "        [3.60555128, 7.21110255]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4c7e9f8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1., 3.],\n",
       "        [2., 4.]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.real(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6aae8857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[2., 5.],\n",
       "        [3., 6.]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.imag(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8c4deb5",
   "metadata": {},
   "source": [
    "### 5.求逆、求行列式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ef8d1a28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5.71579803  4.52713906 -3.83458395  0.07995746 -4.15364409]\n",
      " [ 1.53291327 -0.36152941 -2.22075415  0.80275336  0.7232392 ]\n",
      " [-0.790466    0.08208068 -0.32002112  1.64655352 -0.00970883]\n",
      " [-6.24356309 -4.90019048  6.9669573  -1.6387897   3.94718852]\n",
      " [-3.5934395  -1.58546438  2.64427198 -0.31010632  2.34193538]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "matrix([[ 5.71579803,  4.52713906, -3.83458395,  0.07995746, -4.15364409],\n",
       "        [ 1.53291327, -0.36152941, -2.22075415,  0.80275336,  0.7232392 ],\n",
       "        [-0.790466  ,  0.08208068, -0.32002112,  1.64655352, -0.00970883],\n",
       "        [-6.24356309, -4.90019048,  6.9669573 , -1.6387897 ,  3.94718852],\n",
       "        [-3.5934395 , -1.58546438,  2.64427198, -0.31010632,  2.34193538]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M=np.random.rand(5,5)\n",
    "M=np.matrix(M)\n",
    "\n",
    "print(M.I)#obviously, type'matrix' is simplier\n",
    "np.linalg.inv(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "76ccc551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.030916342663199144"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.det(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00e6486f",
   "metadata": {},
   "source": [
    "### 6.数据处理（统计）\n",
    "\n",
    "对于有多个维度的高维数组，可以用axis来确定要计算哪个范围的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9b247bd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(77431, 7)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "data=np.genfromtxt('stockholm_td_adj.dat')\n",
    "np.shape(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea66cabf",
   "metadata": {},
   "source": [
    "均值mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f798d0b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.800e+03  1.000e+00  1.000e+00 ... -6.100e+00 -6.100e+00  1.000e+00]\n",
      " [ 1.800e+03  1.000e+00  2.000e+00 ... -1.540e+01 -1.540e+01  1.000e+00]\n",
      " [ 1.800e+03  1.000e+00  3.000e+00 ... -1.500e+01 -1.500e+01  1.000e+00]\n",
      " ...\n",
      " [ 2.011e+03  1.200e+01  2.900e+01 ...  4.200e+00  4.200e+00  1.000e+00]\n",
      " [ 2.011e+03  1.200e+01  3.000e+01 ... -1.000e-01 -1.000e-01  1.000e+00]\n",
      " [ 2.011e+03  1.200e+01  3.100e+01 ... -3.300e+00 -3.300e+00  1.000e+00]]\n",
      "6.197109684751585\n"
     ]
    }
   ],
   "source": [
    "print(data)\n",
    "print(np.mean(data[:,3]))#输出气温的平均值，第四列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "771380b0",
   "metadata": {},
   "source": [
    "方差（var/variance）、标准差（std/standard deviation）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6cc5a32c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(68.59602320966341, 8.282271621340573)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(data[:,3]),np.std(data[:,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9f39b42",
   "metadata": {},
   "source": [
    "最值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "74d9a20a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:,3].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fd90a142",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-25.8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:,3].min()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01a546d",
   "metadata": {},
   "source": [
    "求和、积分、累计乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "32738f08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "479848.4"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(data[:,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2d9b2f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3628800"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#给定轴上全元素乘积\n",
    "A=np.arange(1,11)\n",
    "np.prod(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5d1da7ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 12])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B=np.array([[1,2],[3,4]])\n",
    "np.prod(B,axis=1)#第二维两个元素乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "480bc5c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  3  6 10 15 21 28 36 45 55]\n",
      "[      1       2       6      24     120     720    5040   40320  362880\n",
      " 3628800]\n"
     ]
    }
   ],
   "source": [
    "#累计求和和求积\n",
    "print(np.cumsum(A))\n",
    "print(np.cumprod(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6740cc42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对角线求和\n",
    "B=np.array([[1,2,5,8,2],\n",
    "            [3,4,8,9,4],\n",
    "            [7,1,8,3,6],\n",
    "            [7,1,8,3,8],\n",
    "            [1,2,3,4,5]])\n",
    "np.trace(B)#equal to diag(B).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2f76e34",
   "metadata": {},
   "source": [
    "### 7.数组子集的计算\n",
    "\n",
    "**用索引和切片等先提取出子集，然后用相应的函数进行计算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b4ca8c7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800  1  1    -6.1    -6.1    -6.1 1\r\n",
      "1800  1  2   -15.4   -15.4   -15.4 1\r\n",
      "1800  1  3   -15.0   -15.0   -15.0 1\r\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAAOXRFWHRTb2Z0d2F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3E98IQk9+v+zD2NGT1RvZGzgSO690cAH59iLUuW5CDgdcAhVfWTSfdv8E/O/sDhDEbHV3SvZ0y7KN3j5cBpSa4E9gXePN1yRtP9q+VM4HLgqwz+H5+ppQ6SnA78O/CoJDckORI4DjgwyTXAgd32TFjgeP4B2A64oMuC9060JpdskKS2OOKXpMYY/JLUGINfkhpj8EtSYwx+SWqMwS8BSSrJqfO2VyS5ZdSVR7vVPo+et33AcljFVMuDwS8N/BjYO8k23faBwPeWsL8dgKM39yVpGgx+6WfOZbDiKMBhwOkbPujWg/9Yt37655Ps07Uf2623flGSa5O8ovuV44Ddu5tz3tq13X/emv+ndXfWShNn8Es/cwbw3O7BJfvw86unvhH4crd++huAD837bE/g94D9gGO6NZnWAN+uqn2r6rXd9x4LvArYi8GKoPv3eCzSggx+qVNVVwJzDEb752z08W8Cp3bf+wzwoCQP6D5bW1V3VNWtDBYP23mBLr5YVTdU1d3AFV1f0sStmHYB0hbmbAbr2R8APGhe+6amZTasd3LHvLa7WPj/q2G/J/XKEb/0804C3lRVX92o/RLg+TC4Qge4dTPPU7idwSJc0hbHEYc0T1XdwOD5uxs7lsETuq4EfsLPlgheaD/fT/Jv3QO2zwXWjrtWaVSuzilJjXGqR5IaY/BLUmMMfklqjMEvSY0x+CWpMQa/JDXG4Jekxvw/tYNNp3SsDAsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "!head -n 3 stockholm_td_adj.dat\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#np.unique()删除相同的数字然后排序，这里生成一个顺序的1-12月份，循环\n",
    "months = np.unique(data[:,1])\n",
    "#对所有年份求月平均气温，存进列表，绘图\n",
    "monthly_mean = [np.mean(data[data[:,1] == month, 3]) for month in months]\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.bar(months, monthly_mean)\n",
    "ax.set_xlabel(\"Month\")\n",
    "ax.set_ylabel(\"Monthly avg. temp.\");"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
